<?php
/* 
 * This page will be linked from other systems. After linked this page
 * as an javascript, it will inject a central account info bar to the page.
 *
 * WARNING: this page might generate big traffic to the system!
 */
header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
header('Content-Type: application/x-javascript');
// try to let browser cache this for one hour
header ('Expires: ' .gmdate ('D, d M Y H:i:s', time() + 3600). ' GMT');    // Date in the past
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');// always modified
header ('Cache-Control: max-age=3600');  // HTTP/1.1

// dpt id
$dpt_id = empty($_GET['dpt_id'])?0:trim($_GET['dpt_id']);

// max width of generate bar
$width = empty($_GET['width'])?960:trim($_GET['width']);
if ( ! is_numeric($width) ) {
    $width = 960;
}
$baseURL = (empty($_SERVER['HTTPS'])?'http':'https').'://'.$_SERVER['HTTP_HOST'];
$clientAreaURL = $baseURL.'/client/';
//$clientAreaURL = $baseURL;
$message = getDisplayMessage();
?>
function hideLayer(e){
	var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	if (reltg.nodeName=='HTML'){
		document.getElementById('yesup_brands').style.display='none';
		return;
	}
	if (tg.nodeName != 'DIV') return;
	
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return;
		document.getElementById('yesup_brands').style.display='none';
}

function hideLayer1(e){
	var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	if (reltg.nodeName=='HTML'){
		document.getElementById('yesup_brands').style.display='none';
		return;
	}
	if (tg.nodeName != 'P') return;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return;
		document.getElementById('yesup_brands').style.display='none';
}

var YesupCA = {
  ver: "1.0.0",
  hasLoad: false,
  old_onload: window.onload,
  pending_onloads: [],
  bootstrap: function() {
    window.onload = YesupCA.onload_hook;
    this.onload("showBar",{});
  },
  onload: function(method,data) {
    if ( this.hasLoad ) {
      YesupCA.callback(method,data);
    } else {
      this.pending_onloads.push({method:method,data:data});
    }
  },
  onload_hook: function() {
    YesupCA.hasLoad = true;
    for ( var i in YesupCA.pending_onloads) {
      var item = YesupCA.pending_onloads[i];
      YesupCA.callback(item.method, item.data);
    }
    if ( typeof(YesupCA.old_onload) == "function" ) {
      YesupCA.old_onload();
    }
  },
  callback: function(method,data) {
    var f = YesupCA[method];
    if ( typeof(f) == "function" ) {
      f.call(YesupCA, data);
    }
  },
  showSolution: function(){
  	if (( document.getElementById('yesup_brands').style.display=='none' )||(document.getElementById('yesup_brands').style.display=='')){
  		document.getElementById('yesup_brands').style.display='block';
		var el = document.getElementById('yesup_brands');
		
		if (el.addEventListener){
			el.addEventListener("mouseout", function(e){hideLayer(e);}, false);  
		} else if (el.attachEvent){  
			el.attachEvent('onmouseout', function(e){hideLayer(e);});  
		}

		var el1 = document.getElementById('yesup_solutions');
		
		if (el1.addEventListener){
			el1.addEventListener("mouseout", function(e){hideLayer1(e);}, false);  
		} else if (el1.attachEvent){  
			el1.attachEvent('onmouseout', function(e){hideLayer1(e);});  
		}
/*
  	}else{
  		document.getElementById('yesup_brands').style.display='none';*/
  	}
  },
  showBar: function() {
    var css="#yesup_bar {margin:0;padding:0; background:url(<?php echo $baseURL; ?>/images/yesup_bar/bar_bg.jpg) 0 0 repeat-x; color:#fff;     border-bottom:#fff solid 1px; width:100%; overflow:hidden; height:26px; line-height:26px; font-family: arial; font-size:12px; background:url(<?php echo $baseURL; ?>/images/yesup_bar/bg-dark.gif) 0 0 repeat-x; margin-top:-26px; }\
#yesup_bar p { display:inline-block;  margin:0; height:20px; line-height:20px;padding:0;  text-shadow:#000 0 1px 1px;  color:#ccc; font-weight:bold;   }\
#yesup_bar p img { /*float:left;*/ padding-right:10px; }\
#yesup_bar p a { color:#9c0;}\
a { outline:none; }\
a img { border:none; }\
#yesup_bar a img { border:none; outline: none; float:right; margin-right:10px; }\
#yesup_bar a.yesup_login_btn { float:right; display:inline-block; height:18px; width:144px; padding:0 10px; margin:0; background:url(<?php echo $baseURL; ?>/images/yesup_bar/yesup_acc_btn.png) 0 0 no-repeat;}\
#yesup_bar a.yesup_login_btn:hover {background-position:0 -18px; }\
#yesup_bar #yesup_bar_container { padding:3px 10px; height:20px; line-height:20px;  margin: 0 auto; text-align:center; }\
#yesup_bar #yesup_bar_container #yesup_solutions { position:absolute; top:0; left:0; background:url(<?php echo $baseURL; ?>/images/yesup_bar/dots.png) top right no-repeat ;filter:alpha(opacity=80); opacity:0.8;    height:26px; padding:0 15px; line-height:26px; width:160px; text-align:left;     }\
#yesup_bar #yesup_bar_container #yesup_solutions  a { font-size:13px;  color:#fff; text-decoration:none; display:block;  }\
#yesup_bar #yesup_bar_container #yesup_solutions  a:hover { color:#9c0; }\
#yesup_bar #yesup_bar_container  #yesup_brands { position:absolute; z-index:999999; top:26px; left:0;   background:#000; color:#fff; padding:10px 15px;; width:160px; filter:alpha(opacity=85); opacity:0.85; text-align:left; -moz-border-radius-bottomright:15px; display:none;   }\
#yesup_bar #yesup_bar_container  #yesup_brands  a {font-size:12px; display:block; color:#fff; padding:3px 0; text-decoration:none; font-weight:normal;     }\
#yesup_bar #yesup_bar_container  #yesup_brands  a:hover { color:#9c0; }\
#yesup_bar #yesup_bar_container  #yesup_brands  a span { display:inline-block; width:16px; height:16px;\
background:url(<?php echo $baseURL; ?>/images/yesup_bar/i16.png) ; margin-right:10px;  }\
body { padding:0; margin:26px 0 0 0 ;}";
    
    YesupCA.addCss(css);
    var msg = <?php echo json_encode($message); ?>;
    
    var solution='<p id="yesup_solutions" onmouseover="YesupCA.showSolution();return false;">\
  <a href="javascript:void()"> YesUp Solutions <img src="<?php echo $baseURL; ?>/images/yesup_bar/open_arrow.png" style="float:right; margin:0;  padding-top:8px; "/></a>\
</p>\
<div id="yesup_brands">\
<a href="http://www.clicksor.com" target="_blank">\
<span style="background-position:0 -48px;">  </span>\
Clicksor.com</a>\
<a href="http://www.adonion.com"  target="_blank">\
<span style="background-position:0 0;">  </span>\
AdOnion.com</a>\
<a href="http://www.yesadvertising.com/"  target="_blank">\
<span style="background-position:0 -64px;">  </span>\
CpxCenter.com</a>\
<a href="http://www.infinityads.com/"  target="_blank">\
<span style="background-position:0 -96px;">  </span>\
InfinityAds.com</a>\
<a href="http://www.xxxwebtraffic.com/"  target="_blank">\
<span style="background-position:0 -144px;">  </span>\
XXXWebTraffic.com</a>\
<a href="http://www.xtargeting.com/"  target="_blank">\
<span style="background-position:0 -128px;">  </span>\
XTargeting.com</a>\
<!-- a href="http://www.callstats.com/"  target="_blank">\
<span style="background-position:0 -176px;">  </span>\
CallStats.com</a-->\
<a href="http://www.emailserving.com/"  target="_blank">\
<span style="background-position:0 -80px;">  </span>\
EmailServing.com</a>\
<a href="http://www.bannercenter.net/"  target="_blank">\
<span style="background-position:0 -32px;">  </span>\
BannerCenter.net</a>\
<a href="http://seotoolscentral.com/"  target="_blank">\
<span style="background-position:0 -192px;">  </span>\
SeoToolsCentral.com</a>\
<a href="http://www.yesupnet.com/"  target="_blank">\
<span style="background-position:0 -160px;">  </span>\
YesUpNet.com-</a></h5>\
<a href="http://anyemails.com/"  target="_blank">\
<span style="background-position:0 -16px;">  </span>\
AnyEmails.com</a>\
<a href="http://www.interupload.com/"  target="_blank">\
<span style="background-position:0 -112px;">  </span>\
InterUpload.com</a>\
</div>';
            
    var content = '<div id="yesup_bar_container"><a href="<?php echo $clientAreaURL; ?>" target=_blank class="yesup_login_btn"></a>'+solution+msg+' </div>';
    
    try {
      var div = document.createElement("div");
      div.setAttribute("id","yesup_bar");
      div.innerHTML = content;
      document.body.insertBefore(div,document.body.firstChild);
    } catch (ex) {
      document.write('<div id="yesup_bar">');
      document.write(content);
      document.write('</div>');
    }
  },
  addCss: function(css) {
    try {
      var el = document.createElement("style");
      el.type = "text/css";
      if ( el.styleSheet ) {
        el.styleSheet.cssText = css;
      } else {
        var ct = document.createTextNode(css);
        el.appendChild(ct);
      }
      document.getElementsByTagName("head")[0].appendChild(el);
    } catch (ex) {
      document.write('<style type="text/css">');
      document.write(css);
      document.write('</style>');
    }
  }
};

YesupCA.bootstrap();
<?php
/**
 * return the message html displayed on info bar. We should somehow get it from
 * database.
 */
function getDisplayMessage() {
	global $baseURL,$dpt_id;
		
	$message = getMessage($dpt_id);
	if ($message[0]==0)
    	return '<p>'.$message[1].'</p>';
	else
    	return '<p><img src="'.$baseURL.'/images/yesup_bar/20x20_icon_'.$message[0].'.png" >'.$message[1].'</p>';
}


	function getMessage($dpt_id)
	{
		
		$cache_message = apc_fetch('message_'.$dpt_id);		
		                          
		if(!empty($cache_message)){
			//error_log("from cache.");
			return $cache_message;			
		}else{
		
			$message=array();
			
			$link = mysql_connect("192.168.26.139", "yesupacc", "yesupacc");
			if ($link){
			//if ($link = mysql_connect("192.168.26.139", "development", "development")){
				mysql_select_db('account_yesup', $link);
			}else{
				$message[0]=0;
				$message[1]='You can manage all your accounts with YesUp Account.';
				return $message;
			}
			
			$now = date('Y-m-d H:m:s',time());
					
			//for important announcement
			if (!empty($dpt_id)){
				$sql = "select a.news_id,news_content,icon from acc_news as a left join acc_news_service as s on(a.news_id=s.news_id) where a.status='Active' and a.type='Message' and a.important='1' and (s.dpt_id='$dpt_id') ";
		        $sql .=" and ( (a.from_date = '0000-00-00 00:00:00' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date> '$now' ) )";      	
		        $sql .=" and ( a.max_display_times = '0' or ( a.max_display_times != '0' and a.display_times < max_display_times) ) ";
		        $sql .=" order by a.display_times limit 1;"; 
	        	$result = mysql_query($sql, $link);
	        	$num_rows = mysql_num_rows($result);
				if ($num_rows<=0){
					$sql = "select a.news_id,news_content,icon from acc_news as a left join acc_news_service as s on(a.news_id=s.news_id) where a.status='Active' and a.type='Message' and a.important='1' and (s.dpt_id='0') ";
			        $sql .=" and ( (a.from_date = '0000-00-00 00:00:00' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date> '$now' ) )";      	
			        $sql .=" and ( a.max_display_times = '0' or ( a.max_display_times != '0' and a.display_times < max_display_times) ) ";
			        $sql .=" order by a.display_times limit 1;"; 
	        		$result = mysql_query($sql, $link);
				}	
				
			}else{
				$sql = "select a.news_id,news_content,icon from acc_news as a left join acc_news_service as s on(a.news_id=s.news_id) where a.status='Active' and a.type='Message' and a.important='1'";
		        $sql .=" and ( (a.from_date = '0000-00-00 00:00:00' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date> '$now' ) )";      	
		        $sql .=" and ( a.max_display_times = '0' or ( a.max_display_times != '0' and a.display_times < max_display_times) ) ";
		        $sql .=" order by a.display_times limit 1;"; 
	        	$result = mysql_query($sql, $link);
			}
	        $num_rows = mysql_num_rows($result);
			//end of announcement
			
			//for other message
			if ($num_rows<=0){
				if (!empty($dpt_id)){
					$sql = "select a.news_id,news_content,icon from acc_news as a left join acc_news_service as s on(a.news_id=s.news_id) where a.status='Active' and a.type='Message' and (s.dpt_id='$dpt_id') ";
			        $sql .=" and ( (a.from_date = '0000-00-00 00:00:00' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date> '$now' ) )";      	
			        $sql .=" and ( a.max_display_times = '0' or ( a.max_display_times != '0' and a.display_times < max_display_times) ) ";
			        $sql .=" order by rand() limit 1;"; 
		        	$result = mysql_query($sql, $link);
		        	$num_rows = mysql_num_rows($result);
					if ($num_rows<=0){
						$sql = "select a.news_id,news_content,icon from acc_news as a left join acc_news_service as s on(a.news_id=s.news_id) where a.status='Active' and a.type='Message' and (s.dpt_id='0') ";
				        $sql .=" and ( (a.from_date = '0000-00-00 00:00:00' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date> '$now' ) )";      	
				        $sql .=" and ( a.max_display_times = '0' or ( a.max_display_times != '0' and a.display_times < max_display_times) ) ";
				        $sql .=" order by rand() limit 1;"; 
		        		$result = mysql_query($sql, $link);
					}	
					
				}else{
					$sql = "select a.news_id,news_content,icon from acc_news as a left join acc_news_service as s on(a.news_id=s.news_id) where a.status='Active' and a.type='Message' ";
			        $sql .=" and ( (a.from_date = '0000-00-00 00:00:00' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date='0000-00-00 00:00:00') or ( a.from_date < '$now' and a.end_date> '$now' ) )";      	
			        $sql .=" and ( a.max_display_times = '0' or ( a.max_display_times != '0' and a.display_times < max_display_times) ) ";
			        $sql .=" order by rand() limit 1;"; 
		        	$result = mysql_query($sql, $link);
				}
	
		        $num_rows = mysql_num_rows($result);
			}	        	        
	        
			if ($num_rows>0){
				$row = mysql_fetch_array($result);
				$message[0]=$row['icon'];
				$message[1]=stripslashes($row['news_content']);
				$news_id = $row['news_id'];
				$sql = " update acc_news set display_times = display_times+1 where news_id='$news_id' ";
				mysql_query($sql, $link);
			}else{
				$message[0]=0;
				$message[1]='You can manage all your accounts with YesUp Account.';
			}
					
			apc_add('message_'.$dpt_id, $message, 180);  //cached for 3 mins
			
			return $message;
		}
	}

